package sg.edu.nus.iss.billing.parse;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import sg.edu.nus.iss.billing.entity.PaymentHistory;

/**
 * @author sammil
 *
 */
public class PaymentFileParser {

	HashMap<String, ArrayList<PaymentHistory>> outputMap;

	public HashMap<String, ArrayList<PaymentHistory>> parse(
			String inputFileName) throws IOException {
		LineNumberReader in = null;
		try {
			outputMap = new HashMap<String, ArrayList<PaymentHistory>>();
			in = new LineNumberReader(new FileReader(inputFileName));
			ParserState current = PaymentAccountHeaderState.SINGLETON;
			while (current != FinishedState.SINGLETON) {
				System.out.println("current:" + current);
				current = current.parse(in, outputMap);
			}
			return outputMap;
		} finally {
			if (in != null) {
				in.close();
			}
		}
	}

	@SuppressWarnings("unchecked")
	public List<PaymentHistory> getPaymentHistoryByAccount(String accountNo) {
		if (outputMap.containsKey(accountNo)) {
			 ArrayList<PaymentHistory>  paymentHistoryList =  outputMap.get(accountNo);
			 Collections.sort(paymentHistoryList);
			return paymentHistoryList;
		}
		return null;
	}
	/*
	public ArrayList<PaymentHistory> getCallListByAccount(String accountNo,CallType callType) {
		ArrayList<PaymentHistory> callTransFilteredList = new ArrayList<PaymentHistory>();
		if (outputMap.containsKey(accountNo)) {
			ArrayList<PaymentHistory> callTransList = outputMap.get(accountNo);
			if (callTransList != null)
				for (PaymentHistory callTxn : callTransList) {
					if (callTxn.getCallType() == callType) {
						callTransFilteredList.add(callTxn);
					}
				}
		}
		return callTransFilteredList;
	}
	
	public ArrayList<PaymentHistory> getCallListByAccount(String accountNo,String hostNo) {
		ArrayList<PaymentHistory> callTransFilteredList = new ArrayList<PaymentHistory>();
		if (outputMap.containsKey(accountNo)) {
			ArrayList<PaymentHistory> callTransList = outputMap.get(accountNo);
			if (callTransList != null)
				for (PaymentHistory callTxn : callTransList) {
					if (callTxn.getHostNumber().equals(hostNo)) {
						callTransFilteredList.add(callTxn);
					}
				}
		}
		return callTransFilteredList;
	}
	public ArrayList<PaymentHistory> getCallListByAccount(String accountNo,String hostNo, CallType callType) {
		ArrayList<PaymentHistory> callTransFilteredList = new ArrayList<PaymentHistory>();
		if (outputMap.containsKey(accountNo)) {
			ArrayList<PaymentHistory> callTransList = outputMap.get(accountNo);
			if (callTransList != null)
				for (PaymentHistory callTxn : callTransList) {
					if (callTxn.getHostNumber().equals(hostNo)
							&& callTxn.getCallType() == callType) {
						callTransFilteredList.add(callTxn);
					}
				}
		}
		return callTransFilteredList;
	}
*/
	public static void main(String s[]) {

		System.out.println("Start testing");
		String inputFileName = "";
		try {
			inputFileName = (new File(".").getCanonicalPath().concat("/data/payment-data.txt"));
			System.out.println("inputFileName :" + inputFileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PaymentFileParser paymentFileParser = new PaymentFileParser();
		try {
			paymentFileParser.parse(inputFileName);
			List<PaymentHistory> paymentHistoryList = paymentFileParser.getPaymentHistoryByAccount("12345680");
			System.out.println("Size " + paymentHistoryList.size());
			for (PaymentHistory paymentHistory : paymentHistoryList) {
				System.out.println(paymentHistory);
			}


		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Done!");
	}
}
